﻿using CodeKicker.BBCode;
using NUnit.Framework;
using Smarly.Bussines.Parsing;

namespace Smarly.Bussines.Test.BbCode
{
	[TestFixture]
	public class Parsing
	{
		[Test]
		public void PToHtml()
		{
			var bb = "[p]text[/p]";
			var expected = "<p>text</p>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[p]text[/p]", expected.ToBbCode());

			bb = "[p=88]text[/p]";
			expected = "<p id=\"88\">text</p>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[p=88]text[/p]", expected.ToBbCode());

			bb = "[p id=88]text[/p]";
			expected = "<p id=\"88\">text</p>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[p=88]text[/p]", expected.ToBbCode());
		}
		[Test]
		public void ListToHtml()
		{
			string actual;


			actual = "[ulist][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/ulist]".ToHtml();
			Assert.AreEqual("<ul><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ul>", actual, actual);
			Assert.AreEqual("[ulist][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/ulist]", "<ul><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ul>".ToBbCode());

			actual = "[ulist=88][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/ulist]".ToHtml();
			Assert.AreEqual("<ul id=\"88\"><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ul>", actual, actual);
			Assert.AreEqual("[ulist=88][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/ulist]", "<ul id=\"88\"><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ul>".ToBbCode());

			actual = "[olist][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/olist]".ToHtml();
			Assert.AreEqual("<ol><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ol>", actual, actual);
			Assert.AreEqual("[olist][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/olist]", "<ol><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ol>".ToBbCode());

			actual = "[olist=88][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/olist]".ToHtml();
			Assert.AreEqual("<ol id=\"88\"><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ol>", actual, actual);
			Assert.AreEqual("[olist=88][*]jQuery очень выразителен.[/*][*]Методы jQuery относятся к нескольким элементам[/*][/olist]", "<ol id=\"88\"><li>jQuery очень выразителен.</li><li>Методы jQuery относятся к нескольким элементам</li></ol>".ToBbCode());

		}

		[Test]
		public void BToHtml()
		{
			var bb = "[b]bbcode[/b]\n[b]bbcode[/b]";
			var expected = "<strong>bbcode</strong>\n<strong>bbcode</strong>";
			string result = bb.ToHtml();
			Assert.AreEqual(expected, result, result);
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void BrToHtml()
		{
			var bb = "[p]bbcode1[br]bbcode2[/p]";
			var expected = "<p>bbcode1<br/>bbcode2</p>";
			string result = bb.ToHtml();
			Assert.AreEqual(expected, result, result);
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void ImgToHtml()
		{
			var bb = "[img=http://storage.smarly.net/pro-jquery/6.1.png]";
			var expected = "<img src=\"http://storage.smarly.net/pro-jquery/6.1.png\"/>";
			string result = bb.ToHtml();
			Assert.AreEqual(expected, result, result);
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void CodeToHtml()
		{
			var bb = "[code]bbcode[/code]";
			var expected = "<code>bbcode</code>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[pre=css]bbcode[/pre]";
			expected = "<pre><code class=\"css\">bbcode</code></pre>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void BlockquoteToHtml()
		{
			var bb = "[blockquote]bbcode[/blockquote]";
			var expected = "<blockquote>bbcode</blockquote>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void Table1ToHtml()
		{
			string bb;
			string expected;

/*
			bb = "[table][/table]";
			expected = "<table></table>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
*/

			bb = "[table=table table-bordered][/table]";
			expected = "<table class=\"table table-bordered\"></table>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
			/*

						var parser = new BBCodeParser(ErrorMode.ErrorFree, null, new[] { new BBTag("quote", "<div><span>Posted by ${name}</span>", "</div>", new BBAttribute("name", "")) { GreedyAttributeProcessing = true } });

						var input = "[quote=Test User With Spaces]Here is my comment[/quote]";
						var expected = "<div><span>Posted by Test User With Spaces</span>Here is my comment</div>";


						Assert.AreEqual(expected, parser.ToHtml(input));*/
		}

		[Test]
		public void TableToHtml()
		{
			var bb = "[div=109][blockquote][b]Таблица 2-1:[/b] Элементы HTML, используемые в примере[/blockquote][table=table table-bordered][thead][tr][td]Элемент[/td][td]Описание[/td][/tr][/thead][tbody][tr][td][code]DOCTYPE[/code][/td][/tr][/tbody][/table][/div]";
			var expected = "<div id=\"109\"><blockquote><strong>Таблица 2-1:</strong> Элементы HTML, используемые в примере</blockquote><table class=\"table table-bordered\"><thead><tr><td>Элемент</td><td>Описание</td></tr></thead><tbody><tr><td><code>DOCTYPE</code></td></tr></tbody></table></div>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void DivToHtml()
		{
			var bb = "[div]bbcode[/div]";
			var expected = "<div>bbcode</div>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[div=88]text[/div]";
			expected = "<div id=\"88\">text</div>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[div=88]text[/div]", expected.ToBbCode());

			bb = "[div id=88]text[/div]";
			expected = "<div id=\"88\">text</div>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[div=88]text[/div]", expected.ToBbCode());
		}

		[Test]
		public void H1ToHtml()
		{
			var bb = "[h1]bbcode[/h1]";
			var expected = "<h1>bbcode</h1>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h1=88]text[/h1]";
			expected = "<h1 id=\"88\">text</h1>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h1=88]text[/h1]", expected.ToBbCode());

			bb = "[h1 id=88]text[/h1]";
			expected = "<h1 id=\"88\">text</h1>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h1=88]text[/h1]", expected.ToBbCode());
		}

		[Test]
		public void H2ToHtml()
		{
			var bb = "[h2]bbcode[/h2]";
			var expected = "<h2>bbcode</h2>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h2=88]text[/h2]";
			expected = "<h2 id=\"88\">text</h2>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h2=88]text[/h2]", expected.ToBbCode());

			bb = "[h2 id=88]text[/h2]";
			expected = "<h2 id=\"88\">text</h2>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h2=88]text[/h2]", expected.ToBbCode());
		}

		[Test]
		public void H3ToHtml()
		{
			var bb = "[h3]bbcode[/h3]";
			var expected = "<h3>bbcode</h3>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h3=88]text[/h3]";
			expected = "<h3 id=\"88\">text</h3>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h3=88]text[/h3]", expected.ToBbCode());

			bb = "[h3 id=88]text[/h3]";
			expected = "<h3 id=\"88\">text</h3>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h3=88]text[/h3]", expected.ToBbCode());
		}

		[Test]
		public void H4ToHtml()
		{
			var bb = "[h4]bbcode[/h4]";
			var expected = "<h4>bbcode</h4>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h4=88]text[/h4]";
			expected = "<h4 id=\"88\">text</h4>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h4=88]text[/h4]", expected.ToBbCode());

			bb = "[h4 id=88]text[/h4]";
			expected = "<h4 id=\"88\">text</h4>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h4=88]text[/h4]", expected.ToBbCode());
		}

		[Test]
		public void H5ToHtml()
		{
			var bb = "[h5]bbcode[/h5]";
			var expected = "<h5>bbcode</h5>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h5=88]text[/h5]";
			expected = "<h5 id=\"88\">text</h5>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h5=88]text[/h5]", expected.ToBbCode());

			bb = "[h5 id=88]text[/h5]";
			expected = "<h5 id=\"88\">text</h5>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h5=88]text[/h5]", expected.ToBbCode());
		}

		[Test]
		public void H6ToHtml()
		{
			var bb = "[h6]bbcode[/h6]";
			var expected = "<h6>bbcode</h6>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[h6=88]text[/h6]";
			expected = "<h6 id=\"88\">text</h6>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h6=88]text[/h6]", expected.ToBbCode());

			bb = "[h6 id=88]text[/h6]";
			expected = "<h6 id=\"88\">text</h6>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h6=88]text[/h6]", expected.ToBbCode());
		}

		[Test]
		public void IToHtml()
		{
			var bb = "[i]bbcode[/i]";
			var expected = "<em>bbcode</em>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());
		}

		[Test]
		public void UrlToHtml()
		{
			var bb = "[url=www.smarly.net]bbcode[/url]";
			var expected = "<a href=\"www.smarly.net\">bbcode</a>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[url=www.smarly.net]bbcode[/url]", expected.ToBbCode());

			bb = "[url]bbcode[/url]";
			expected = "<a href=\"\">bbcode</a>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[url]bbcode[/url]", expected.ToBbCode());
		}

		[Test]
		public void UlToHtml()
		{
			var bb = "[ul][li]bb[/li][li]code[/li][/ul]";
			var expected = "<ul><li>bb</li><li>code</li></ul>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

			bb = "[ol][li]bb[/li][li]code[/li][/ol]";
			expected = "<ol><li>bb</li><li>code</li></ol>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual(bb, expected.ToBbCode());

/*
			bb = "[h4=88]text[/h4]";
			expected = "<h4 id=\"88\">text</h4>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h4=88]text[/h4]", expected.ToBbCode());

			bb = "[h4 id=88]text[/h4]";
			expected = "<h4 id=\"88\">text</h4>";
			Assert.AreEqual(expected, bb.ToHtml());
			Assert.AreEqual("[h4=88]text[/h4]", expected.ToBbCode());
*/
		}


	}

}